//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;

namespace CommunityServer.Components
{
	/// <summary>
	/// Summary description for ThreadQuery.
	/// </summary>
	public abstract class ThreadQuery
	{
		public ThreadQuery(){}

		#region Query Properties
		
		/// <summary>
		/// SectionID to filter by
		/// </summary>
		public int SectionID = -1;

		/// <summary>
        /// SectionGroupID to filter by
        /// </summary>
        public int GroupID = -1;

		/// <summary>
		/// Post to filter by
		/// </summary>
		public int PostID = -1;
		
		/// <summary>
		/// Post to filter by
		/// </summary>
		public int[] MultiplePostIDs = null;

		/// <summary>
		/// Parent Post to filter by
		/// </summary>
		public int ParentID = -1;

		/// <summary>
		/// Only return items if the section is active
		/// </summary>
		public bool RequireSectionIsActive = true;

		/// <summary>
		/// Post to filter by name
		/// </summary>
        public string PostName = null;
		
		/// <summary>
		/// Ignore paging and return the entire result set
		/// </summary>
		public bool ReturnFullThread = false;

		/// <summary>
		/// Total Records Returned by the Query
		/// </summary>
		public int TotalRecords;

		/// <summary>
		/// Current Page
		/// </summary>
		public int PageIndex = 0;

		/// <summary>
		/// # of threads per request
		/// </summary>
		public int PageSize = 20;

		/// <summary>
		/// CategoryID to filter by
		/// </summary>
		public int CategoryID = 0;

		/// <summary>
		/// Only return posts not in any category
		/// </summary>
		public bool UncategorizedOnly = false;

		/// <summary>
		/// User to filter by
		/// </summary>
		public int UserID = 0;

		/// <summary>
		/// Date to filter by. Generally relies on BlogThreadType property
		/// </summary>
		public DateTime DateFilter = DateTime.MinValue;

		/// <summary>
		/// How to sort
		/// </summary>
		public SortOrder SortOrder = SortOrder.Ascending;

		/// <summary>
		/// Should the category data be returned as a seperate table.
		/// </summary>
		public bool IncludeCategories = false;

		/// <summary>
		/// Should the pageindex data be returned as a seperate table.
		/// </summary>
		public bool IncludePageIndex = false;

		public ArrayList FilterByList = null;

		/// <summary>
		/// Always reutrns just the first page
		/// </summary>
		public bool FirstPageOnly = false;

		/// <summary>
		/// Flag to let a data provider know it is safe to 
		/// serve the request from a secondary datasource
		/// </summary>
		public bool IsCacheable = true;

		/// <summary>
		/// Flag to let the data provider know this is a request for aggregate 
		/// data
		/// </summary>
		public bool IsAggregate = false;

		/// <summary>
		/// Filter on whether a post has particular media type(s) linked to it
		/// </summary>
		public PostMediaType PostMedia = PostMediaType.Empty;

		public string[] Tags = null;

		public bool LogicallyOrTags = false;
		
		public PostStatus PostStatus = PostStatus.Ignore;

		public int MinimumSpamScore = -1;
		public int MaximumSpamScore = -1;
		

		#endregion
		
		#region Has/Helpers
		/// <summary>
		/// Are we filtering by a date?
		/// </summary>
		public bool FilterByDate { get{return DateFilter > DateTime.MinValue;} }

		/// <summary>
		/// Are we filtering by a category?
		/// </summary>
		public bool FilterByCategory { get{return CategoryID > 0;} }

		/// <summary>
		/// Are we filtering by a specific post?
		/// </summary>
		public bool FilterByPost { get{return PostID > -1;} }

		/// <summary>
		/// Are we filtering by multiple postIDs?
		/// </summary>
		public bool FilterByMultiplePosts { get{return MultiplePostIDs != null && MultiplePostIDs.Length > 0;} }

		/// <summary>
		/// Are we filtering by a specific group?
		/// </summary>
		public bool FilterByGroup { get{return GroupID > -1;} }

		/// <summary>
		/// Are we filtering by a specific sectionID?
		/// </summary>
		public bool FilterBySection { get{return SectionID > -1;} }

		/// <summary>
		/// Are we filtering by a specific post name?
		/// </summary>
		public bool FilterByPostName { get{return !Globals.IsNullorEmpty(PostName);} }
		
		/// <summary>
		/// Are we filtering by a specific parent post?
		/// </summary>
		public bool FilterByParent { get{return ParentID > -1;} }
		
		public bool FilterBySpamScore
		{
			get
			{
				return MinimumSpamScore > -1 || MaximumSpamScore > -1;
			}
		}
		
		/// <summary>
		/// Are we filtering by a specific UserID?
		/// </summary>
		public bool FilterByUserID { get{return UserID > 0;} }

		public bool FilterByTags { get {return Tags != null && Tags.Length > 0;} }
		
		string _filterKey = null;
		public string FilterKey
		{
			get
			{
				if(_filterKey == null)
					if(FilterByList != null && FilterByList.Count > 0)
					{
						string[] ids = new string[FilterByList.Count];
						for(int i = 0; i < FilterByList.Count; i++)
							ids[i] = ((Section)FilterByList[i]).SectionID.ToString();

						_filterKey = string.Join(",", ids);
					}
					else
						_filterKey = string.Empty;

				return _filterKey;
			}
		}

        public bool HasFilterByList
        {
            get { return FilterByList != null && FilterByList.Count > 0;}
        }

		#endregion
	}
}
